package JDBC的复习;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.function.Consumer;

public class JDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //注册mysql驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取连接
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/学生库?useSSL=false", "root", "mysql");
        //定义sql语句
        String sql = "SELECT * FROM tb_brand";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        ArrayList<Brand> list = new ArrayList<>();
        while (resultSet.next()) {
            //获取sql内容
            String name = resultSet.getString(2);
            String companyName = resultSet.getString(3);
            int ordered = resultSet.getInt(4);
            String description = resultSet.getString(5);
            int status = resultSet.getInt(6);
            //封装成对象,在存入集合中
            Brand brand=new Brand(name, companyName,ordered,description,status);
            list.add(brand);
    }
        System.out.println(list);
        /*List<Brand> list2 = list.stream().filter(s -> s.getName().equals("苹果")).toList();
        System.out.println(list2);*/
        output(list);
}

    /**
     * 把集合存入本地文件中
     */
    public static void output(ArrayList<Brand> liat){
        try {
            FileOutputStream fos=new FileOutputStream("D:\\idea2022\\Engineerclass\\src\\Brand.txt");
            liat.forEach(new Consumer<Brand>() {
                @Override
                public void accept(Brand brand) {
                    try {
                        fos.write(brand.toString().getBytes());
                        fos.write("\n".getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
class Brand{
    private String name;
    private String companyName;
    private int or;
    private String description;
    private int Status;

    public Brand() {
    }

    public Brand(String name, String companyName, int or, String description, int Status) {
        this.name = name;
        this.companyName = companyName;
        this.or = or;
        this.description = description;
        this.Status = Status;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return companyName
     */
    public String getCompanyName() {
        return companyName;
    }

    /**
     * 设置
     * @param companyName
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     * 获取
     * @return or
     */
    public int getOr() {
        return or;
    }

    /**
     * 设置
     * @param or
     */
    public void setOr(int or) {
        this.or = or;
    }

    /**
     * 获取
     * @return description
     */
    public String getDescription() {
        return description;
    }

    /**
     * 设置
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * 获取
     * @return Status
     */
    public int getStatus() {
        return Status;
    }

    /**
     * 设置
     * @param Status
     */
    public void setStatus(int Status) {
        this.Status = Status;
    }

    public String toString() {
        return "Brand{name = " + name + ", companyName = " + companyName + ", or = " + or + ", description = " + description + ", Status = " + Status + "}";
    }
}